package xiancheng.demo04;

import java.math.BigDecimal;

/**
 * @author Mr.ma
 * @since 2022/10/31 14:12
 **/
public class Test04 {
    public static void main(String[] args) {
        BankCard bankCard = new BankCard(BigDecimal.ZERO);
        Boy boy = new Boy(bankCard);
        Girl girl = new Girl(bankCard);
        Thread boyThread = new Thread(boy);
        Thread girlThread = new Thread(girl);
        boyThread.start();
        girlThread.start();
    }
}
class Boy implements Runnable{
    private BankCard bankCard;
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            getBankCard().setBalance(getBankCard().getBalance().add(BigDecimal.valueOf(1000)));
            System.out.println(Thread.currentThread().getName() + "存入1000，余额为：" + getBankCard().getBalance());
        }
    }

    public Boy() {
    }

    public Boy(BankCard bankCard) {
        this.bankCard = bankCard;
    }

    public BankCard getBankCard() {
        return bankCard;
    }

    public void setBankCard(BankCard bankCard) {
        this.bankCard = bankCard;
    }
}
class Girl implements Runnable{
    private BankCard bankCard;
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if (getBankCard().getBalance().compareTo(BigDecimal.ZERO) > 0){
                getBankCard().setBalance(getBankCard().getBalance().subtract(BigDecimal.valueOf(1000)));
                System.out.println(Thread.currentThread().getName() + "取出1000，余额为：" + getBankCard().getBalance());
            } else {
                System.out.println("余额不足");
                i--;
            }
        }
    }
    public Girl() {
    }

    public Girl(BankCard bankCard) {
        this.bankCard = bankCard;
    }

    public BankCard getBankCard() {
        return bankCard;
    }

    public void setBankCard(BankCard bankCard) {
        this.bankCard = bankCard;
    }
}